﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lesson11_code2 : MonoBehaviour
{
	public Transform target;
	//z轴偏移
	public float zOffset = 2;
	//y轴偏移
	public float yOffset = 0.5f;

	private Vector3 targetPos;
	public float moveSpeed;
	private Vector3 startPos;
	private float time;

	private Quaternion targetQ;
	public float roundSpeed = 1;
	private float roundTime;
	private Quaternion startQ;


	void Start()
    {
		targetQ = Quaternion.LookRotation(target.position - this.transform.position);
	}

    //摄像机相关的处理放在LateUpdate后处理
    void LateUpdate()
	{
		//匀速移动
		if (targetPos != target.position + -target.forward * zOffset + target.up * yOffset)
		{
			targetPos = target.position + -target.forward * zOffset + target.up * yOffset;
			startPos = this.transform.position;
			time = 0;
		}
		time += Time.deltaTime;
		this.transform.position = Vector3.Lerp(startPos, targetPos, time * moveSpeed);

        if (targetQ != Quaternion.LookRotation(target.position - this.transform.position))
        {
            targetQ = Quaternion.LookRotation(target.position - this.transform.position);
            roundTime = 0;
            startQ = this.transform.rotation;
        }
        roundTime += Time.deltaTime;
        this.transform.rotation = Quaternion.Slerp(startQ, targetQ, roundTime * roundSpeed);
    }
}
